package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.dto.ProjectSettlementPayNodeDto;
import com.yunyao.common.dto.SupplierSettlementPayNodeDto;
import com.yunyao.common.web.enums.ResultMsgEnum;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.BamuReconciliationSettlementPayNode;
import com.yunyao.dao.model.BamuSpReconciliationSettlementPayNode;
import com.yunyao.dao.service.bamu.IBamuReconciliationSettlementPayNodeService;
import com.yunyao.dao.service.bamu.IBamuSpReconciliationSettlementPayNodeService;
import com.yunyao.framework.dto.project.ProjectSettlementPayNodeSaveDto;
import com.yunyao.framework.dto.supplier.SupplierSettlementPayNodeSaveDto;
import com.yunyao.framework.service.ProjectSettlementPayNodeService;
import com.yunyao.framework.service.SupplierSettlementPayNodeService;
import com.yunyao.framework.vo.project.ProjectSettlementPayNodeInfoVO;
import com.yunyao.framework.vo.project.ProjectSettlementPayNodeVO;
import com.yunyao.framework.vo.supplier.SupplierSettlementPayNodeInfoVO;
import com.yunyao.framework.vo.supplier.SupplierSettlementPayNodeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProjectSettlementPayNodeServiceImpl implements ProjectSettlementPayNodeService {
    @Autowired
    private IBamuReconciliationSettlementPayNodeService iBamuSpReconciliationSettlementPayNodeService;

    @Override
    public YResult<PageResult<ProjectSettlementPayNodeVO>> pageList(ProjectSettlementPayNodeDto supplierSettlementPayNodeDto) {
        LambdaQueryWrapper<BamuReconciliationSettlementPayNode> queryWrapper =
                new LambdaQueryWrapper<BamuReconciliationSettlementPayNode>()
                        .eq(StringUtils.isNotBlank(supplierSettlementPayNodeDto.getSettlementId()), BamuReconciliationSettlementPayNode::getSettlementId, supplierSettlementPayNodeDto.getSettlementId())
                        .eq(StringUtils.isNotBlank(supplierSettlementPayNodeDto.getContractId()), BamuReconciliationSettlementPayNode::getContractId, supplierSettlementPayNodeDto.getContractId())
//                        .eq(StringUtils.isNotBlank(supplierSettlementPayNodeDto.getOrderId()), BamuReconciliationSettlementPayNode::getProjectId, supplierSettlementPayNodeDto.getProjectId())
                        .eq(BamuReconciliationSettlementPayNode::getIsDeleted, DataDeletedStatus.NO.getType());

        // 分页查询
        IPage<BamuReconciliationSettlementPayNode> payPage = new Page<>(supplierSettlementPayNodeDto.getPageIndex(), supplierSettlementPayNodeDto.getPageSize());
        payPage = iBamuSpReconciliationSettlementPayNodeService.page(payPage, queryWrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(payPage.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<ProjectSettlementPayNodeVO> materialVOList = materialDaoToVoList1(payPage.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(materialVOList, payPage.getCurrent(), payPage.getSize(), payPage.getPages(), payPage.getTotal()));

    }

    @Override
    public YResult<ProjectSettlementPayNodeInfoVO> getInfo(ProjectSettlementPayNodeDto supplierSettlementPayNodeDto) {
        // 创建查询条件，用于筛选未被删除且ID匹配的结算记录
        LambdaQueryWrapper<BamuReconciliationSettlementPayNode> queryWrapper =
                new LambdaQueryWrapper<BamuReconciliationSettlementPayNode>()
                        .eq(BamuReconciliationSettlementPayNode::getIsDeleted,
                                DataDeletedStatus.NO.getType()).eq(BamuReconciliationSettlementPayNode::getId, supplierSettlementPayNodeDto.getId());

        // 使用查询条件获取第一个匹配的结算记录
        BamuReconciliationSettlementPayNode bamuSpReconciliationSettlement =
                iBamuSpReconciliationSettlementPayNodeService.getOne(queryWrapper);

        // 如果结算记录为空，返回错误结果，表示没有数据
        if (Objects.isNull(bamuSpReconciliationSettlement)) {
            return YResult.success();
        }

        // 将获取到的结算记录转换为供应商结算信息VO，并返回成功结果
        return YResult.success(daoToInfoVo(bamuSpReconciliationSettlement));
    }

    @Override
    public YResult<String> saveInfo(ProjectSettlementPayNodeSaveDto saveDto) {
        BamuReconciliationSettlementPayNode bamuSoSupply = new BamuReconciliationSettlementPayNode();
        BeanUtils.copyProperties(saveDto, bamuSoSupply);
        iBamuSpReconciliationSettlementPayNodeService.saveOrUpdate(bamuSoSupply);
        return YResult.success(bamuSoSupply.getId());
    }

    private ProjectSettlementPayNodeInfoVO daoToInfoVo(BamuReconciliationSettlementPayNode bamuSpReconciliationSettlement) {
        ProjectSettlementPayNodeInfoVO supplierSettlementInfoVO = new ProjectSettlementPayNodeInfoVO();
        BeanUtils.copyProperties(bamuSpReconciliationSettlement, supplierSettlementInfoVO);
        return supplierSettlementInfoVO;
    }
    private List<ProjectSettlementPayNodeVO> materialDaoToVoList1(List<BamuReconciliationSettlementPayNode> records) {
        return records.stream().map(this::materialDaoToVo1).collect(Collectors.toList());
    }

    private ProjectSettlementPayNodeVO materialDaoToVo1(BamuReconciliationSettlementPayNode bamuSpReconciliationOrderMaterial) {
        ProjectSettlementPayNodeVO spReconciliationMaterialVO = new ProjectSettlementPayNodeVO();
        BeanUtils.copyProperties(bamuSpReconciliationOrderMaterial, spReconciliationMaterialVO);
        return spReconciliationMaterialVO;
    }
}
